package com.example.baseweather.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

import com.example.baseweather.domain.MusicInfoBean;

import java.util.List;

public class MediaPlayerViewModel extends ViewModel {

    private List<MusicInfoBean> musicInfoBeans;

    private final MutableLiveData<Boolean> pauseStatusLiveData = new MutableLiveData<>();

    private final MutableLiveData<Integer> currentPlayIndex = new MutableLiveData<>();

    private LiveData<MusicInfoBean> currentMusicInfo;

    public MediaPlayerViewModel(List<MusicInfoBean> musicInfoBeans) {
        this.musicInfoBeans = musicInfoBeans;
        currentMusicInfo = Transformations.map(currentPlayIndex, musicInfoBeans::get);

    }

    public LiveData<Boolean> getPlayStatus() {
        pauseStatusLiveData.setValue(false);
        return pauseStatusLiveData;
    }

    public void setPlayStatus(Boolean pausedStatus) {
        pauseStatusLiveData.setValue(pausedStatus);
    }


    public LiveData<MusicInfoBean> getCurrentMusicInfo() {
        return currentMusicInfo;
    }


    public void getNextMusic() {
        Integer preValue = currentPlayIndex.getValue();
        if (preValue != null) {
            int currentValue = (preValue + 1) % musicInfoBeans.size();
            currentPlayIndex.setValue(currentValue);
        } else {
            currentPlayIndex.setValue(0);
        }

    }

    public void getPreMusic() {
        Integer preValue = currentPlayIndex.getValue();
        if (preValue != null) {
            int currentValue = (preValue - 1 + musicInfoBeans.size()) % musicInfoBeans.size();
            currentPlayIndex.setValue(currentValue);
        } else {
            currentPlayIndex.setValue(0);
        }
    }


}



